package com.example.presentshopping.ui.fragment

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseFragment
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.ProductListFragmentBean
import com.example.presentshopping.config.bean.SpecialOrderBean
import com.example.presentshopping.config.bean.YDClassifyItemBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.activity.DetailsActivity
import com.example.presentshopping.ui.custom.RoundImageView
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.android.synthetic.main.activity_experience.*

class ProductListFragment(
    override var context: Context,
    var str: YDClassifyItemBean.DataBean.ListBean
) : BaseMvpFragment() {

    var pListFragmentBean :  ProductListFragmentBean ?=null

    var Special_recy: SwipeRecyclerView? = null

    var baseRecyclerAdapter: BaseRecyclerAdapter<ProductListFragmentBean.DataBean.ListBean>? = null

    var page =1
    private var sendMesPresenter: PublicPresenter? = null
    override fun initView(view: View?, savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(context, false, "")
        sendMesPresenter?.attachView(this)
        Special_recy = view?.findViewById(R.id.recycler_view)
        setAdapter()
        getCateGoodsList()
        Special_recy?.useDefaultLoadMore()
        Special_recy?.loadMoreFinish(false, true)
        Special_recy?.setLoadMoreListener {
            page++
            getCateGoodsList()
        }
    }
   fun setAdapter(){
       baseRecyclerAdapter = object :BaseRecyclerAdapter<ProductListFragmentBean.DataBean.ListBean>(context,null ,R.layout.item_discover){
           override fun convert(
               holder: BaseRecyclerHolder?,
               data: ProductListFragmentBean.DataBean.ListBean,
               position: Int,
               isScrolling: Boolean,
               selectedPosition: Int
           ) {
               val ivimage = holder?.getView<RoundImageView>(R.id.iv_image)
               val tv_name =holder?.getView<TextView>(R.id.tv_name)
               val tv_description =holder?.getView<TextView>(R.id.tv_description)
               val myodtunbsws =holder?.getView<LinearLayout>(R.id.myodtunbsws)
               tv_name?.text =data.title
               tv_description?.text = "¥: "+data.marketprice
               GlideUtil.load(context,data.thumb,ivimage)
               myodtunbsws?.setOnClickListener {
                   val Init = Intent(context, DetailsActivity().javaClass)
                   Init.putExtra("msid",data.id.toString())
                   Init.putExtra("Activity","ProductListFragment")
                   context?.startActivity(Init)
               }

           }

       }

       Special_recy?.adapter = baseRecyclerAdapter
   }

    fun  getCateGoodsList(){
        sendMesPresenter?.netWorkRequestPost(HttpURL.Req_getCateGoodsList,HttpRequest.getCateGoods(str.id.toString(),"",page))
    }

    override fun onDestroyView() {
        sendMesPresenter?.detachView()
        super.onDestroyView()
    }


    override fun onSuccess(data: String?) {
        pListFragmentBean   = GsonUtils.getGson().fromJson(data,ProductListFragmentBean().javaClass)

        if (pListFragmentBean?.data?.list?.size!!>0){
            recycler_view.loadMoreFinish(false, true)
            baseRecyclerAdapter?.setData(pListFragmentBean?.data?.list)
        }else{
            Special_recy?.loadMoreFinish(true, false)
        }
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override val layoutId: Int
        get() = R.layout.productlis_fragment
}